package com.hainiu.cat.web.controller;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hainiu.cat.web.vo.CustomerVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

/**
 * create by biji.zhao on 2021/6/1
 */
@RestController
@RequestMapping("/vipSearch")
public class VipSearchController {

    private ExecutorService executorService = Executors.newFixedThreadPool(2);

    @GetMapping("/search")
    public Map<String, String> search() {
        Callable<String> textCallable = () -> "text";
        Callable<String> regionCallable = () -> "region";
        // 不需要线程池
        FutureTask<String> textFuture = new FutureTask<>(textCallable);
        FutureTask<String> regionFuture = new FutureTask<>(regionCallable);
        new Thread(textFuture).start();
        new Thread(regionFuture).start();
        String textRes;
        String regionRes;
        try {
            textRes = textFuture.get();
            regionRes = regionFuture.get();
        }catch (Exception e) {
            return Maps.newHashMap();
        }
        return ImmutableMap.of("text", textRes, "region", regionRes);
    }

    @GetMapping("/searchAll")
    public DeferredResult<Map<String, String>> searchAll() {
        DeferredResult<Map<String, String>> result = new DeferredResult<>();
        executorService.submit(() -> {
            result.setResult(ImmutableMap.of("text", "1", "region", "2"));
        });
        return result;
    }

    public static void main(String[] args) {
        System.out.println(0.01 + 0.05);

        String format = new DecimalFormat("0.00").format(4.01234);
        System.out.println(format);

        BigDecimal res = BigDecimal.valueOf(153.54).divide(BigDecimal.valueOf(100), 2);
        System.out.println(res);

    }

}
